package sk.mka.utils.tools.filesystem;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;

/**
 *
 * @author katrami
 * @date Aug 23, 2010
 */
public class FileUtils {

    private static final Logger log = Logger.getLogger(FileUtils.class);

    public static void copy(String from, String to) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            InputStream inFile = new FileInputStream(from);
            in = new BufferedInputStream(inFile);
            OutputStream outFile = new FileOutputStream(to);
            out = new BufferedOutputStream(outFile);
            while (true) {
                int data = in.read();
                if (data == -1) {
                    break;
                }
                out.write(data);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    public static final String getUserDirectory() {
        return System.getProperty("user.dir");
    }

    public static final String getLoggedUser() {
        String user = null;
        String absPath = System.getProperty("user.dir");
        String folders[] = absPath.split("/");

        int j = 0;
        for (int i = 0; i < folders.length; i++) {
            String string = folders[i];
            if (!string.isEmpty()) {
                j++;
                if (j == 2) {
                    user = string;
                    break;
                }
            }
        }
        return user;
    }

    public static void delete(final String fileName) {

        // A File object to represent the filename
        File f = new File(fileName);

        // Make sure the file or directory exists and isn't write protected
        if (f.exists()) {

            if (!f.canWrite()) {
                log.fatal("Delete: write protected: "
                        + fileName);
            }

            // If it is a directory, make sure it is empty
            if (f.isDirectory()) {
                String[] files = f.list();
                if (files.length > 0) {
                    throw new IllegalArgumentException(
                            "Delete: directory not empty: " + fileName);
                }
            }

            // Attempt to delete it
            boolean success = f.delete();


            if (!success) {
                log.fatal("Deletion failed: " + fileName);
            } else {
                log.info("deletion successful:  " + fileName);
            }
        }
    }

    public static BufferedWriter openFileForWriting(final String filename, boolean append) throws IOException {
        FileWriter ofstream = null;
        //open the same file
        BufferedWriter out = null;
        ofstream = new FileWriter(filename, append);
        out = new BufferedWriter(ofstream);
        return out;
    }
}
